package com.smcdd.cdb.dbo.cdb_dbo.ui.activity;

import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.smcdd.cdb.dbo.cdb_dbo.R;

/**
 * 作者：Administrator on 2017/8/9 17:37
 * 邮箱：zhanghuaiha@gmail.com
 * 分组信息
 */

public class GroupingTwoActivity extends ExpandableListActivity {
    private ExpandableListAdapter adapter;
    private TextView title;
    private ImageView back;
//private MyExpandableListAdapter MyAdapter=new MyExpandableListAdapter();

    /* (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        adapter = new MyExpandableListAdapter();
        setListAdapter(adapter);
    }

    /* (non-Javadoc)
     * @see android.app.Activity#onContextItemSelected(android.view.MenuItem)
     */
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        boolean flag = false;
        // TODO Auto-generated method stub
        ExpandableListView.ExpandableListContextMenuInfo menuInfo = (ExpandableListView.ExpandableListContextMenuInfo) item.getMenuInfo();
        String title = ((TextView) menuInfo.targetView).getText().toString();
        int type = ExpandableListView.getPackedPositionType(menuInfo.packedPosition);

        if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
            int groupPos = ExpandableListView.getPackedPositionGroup(menuInfo.packedPosition);
            int childPos = ExpandableListView.getPackedPositionChild(menuInfo.packedPosition);

            CharSequence str = "您单击了" + title;
            Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
            Log.i("tag", "Run Hereing...");

            flag = true;
        } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
            int groupPos = ExpandableListView.getPackedPositionGroup(menuInfo.packedPosition);
            CharSequence cs = "您单击了" + title;
            Toast.makeText(this, cs, Toast.LENGTH_SHORT).show();
            Log.i("tag", "Run Here...");
            flag = true;
        }
        return flag;
    }

    public class MyExpandableListAdapter extends BaseExpandableListAdapter {
        // Sample data set.  children[i] contains the children (String[]) for groups[i].
        public String[] groups = {"我的好友", "新疆同学", "亲戚", "同事"};
        public String[][] children = {
                {"胡算林", "张俊峰", "王志军", "二人", "胡算林", "张俊峰", "王志军", "二人", "胡算林", "张俊峰", "王志军", "二人", "胡算林", "张俊峰", "王志军", "二人", "胡算林", "张俊峰", "王志军", "二人", "胡算林", "张俊峰", "王志军", "二人", "胡算林", "张俊峰", "王志军", "二人", "胡算林", "张俊峰", "王志军", "二人", "胡算林", "张俊峰", "王志军", "二人"},
                {"李秀婷", "蔡乔", "别高", "余音"},
                {"摊派新", "张爱明"},
                {"马超", "司道光"}
        };

        public Object getChild(int groupPosition, int childPosition) {
            return children[groupPosition][childPosition];
        }

        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        public int getChildrenCount(int groupPosition) {
            return children[groupPosition].length;
        }

        public TextView getGenericView() {
            // Layout parameters for the ExpandableListView
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, 64);

            TextView textView = new TextView(GroupingTwoActivity.this);
            textView.setLayoutParams(lp);
            // Center the text vertically
            textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
            // Set the text starting position
            textView.setPadding(36, 0, 0, 0);
            return textView;
        }

        //【重要】填充二级列表
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                                 View convertView, ViewGroup parent) {
            TextView textView = getGenericView();
            textView.setText(getChild(groupPosition, childPosition).toString());
            return textView;
        }

        public Object getGroup(int groupPosition) {
            return groups[groupPosition];
        }

        public int getGroupCount() {
            return groups.length;
        }

        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        //【重要】填充一级列表
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
                                 ViewGroup parent) {
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.item_grouping_parent, null);
            } else {

            }
            TextView tv_group = (TextView) convertView.findViewById(R.id.tv_parent);
            tv_group.setText(getGroup(groupPosition).toString());
//            TextView textView = getGenericView();
//            textView.setText(getGroup(groupPosition).toString());
            return convertView;
        }

        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

        public boolean hasStableIds() {
            return true;
        }

    }

}
